<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAutoload()
    {
        $moduleLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath' => APPLICATION_PATH));
        
        // Add models & forms resources
        $moduleLoader->addResourceTypes(array(
            'models' => array(
                'path'      => 'models',
                'namespace' => 'Core'   
            ),
            'forms' => array(
                'path'     => 'forms',
                'namespace' => 'Forms'
            ),
        ));
        
        return $moduleLoader;
    }

    /*
     * Function to inicializate the database
     */
    protected function _initDatabase()
    {
        $database = $this->getOption('database');
        try {
            $db = Zend_Db::factory($database['adapter'], $database['params']);
            $db->getConnection();
            Zend_Db_Table_Abstract::setDefaultAdapter($db);
        } catch (Zend_Db_Adapter_Exception $e) {
            die($e->getMessage());
        } catch (Zend_Exception $e) {
            die($e->getMessage());
        }
    }

    protected function _initViewHelpers()
    {
        /*
         * Theme of the site
         *  => Note: The value $theme must get from the DB not application.ini
         */
        $theme = $this->getOption('theme');
        $session = new Zend_Session_Namespace('Default');
        $session->theme = $theme['name'];

        // Inicializar el MVC
        $option = array(
            'layoutPath' => PUBLIC_PATH.'/'.$session->theme,
            'layout' => 'layout'
        );

        Zend_Layout::startMvc($option);
        $layout = Zend_Layout::getMvcInstance();
        $view = $layout->getView();
        $view->addHelperPath("App/View/Helper", "App_View_Helper");
        // Set doctype
        $view->doctype("HTML5");
        // Set default directory view per module name
        App_View_Helper_View::setViewModuleDirectory('default');
       
    }
    
    /*
     * Inicialize ACL
     */
    protected function _initACL()
    {

        $acl = new App_ACL_Initialize();
        //var_dump($acl->iniACL());
        //$acl->setRoles();
        //$acl->setResources();
        //$acl->setPrivilages();
        //$acl->setAcl();
        //$frontController->registerPlugin(new Extras_ACL_Register());
        
    }
    
    
    /*
     * Inicialize route
     */
    protected function _initRouter()
    {
        
  
        # Routes
        $frontController = Zend_Controller_Front::getInstance();
        $router = $frontController->getRouter();


        /*
         * Route for edit user
         */
        $route = new Zend_Controller_Router_Route(
                        'admin/user/list/:sortField/:sortDir',
                        array(
                            'module' => 'admin',
                            'controller' => 'user',
                            'action' => 'list'
                        )
        );
        $router->addRoute('admin_user_index', $route);

        /*
         * Route for edit user
         */
        $route = new Zend_Controller_Router_Route(
                        'admin/user/edit/:userID',
                        array(
                            'module' => 'admin',
                            'controller' => 'user',
                            'action' => 'edit'
                        )
        );
        $router->addRoute('admin_user_edit', $route);

        /*
         * Route for delete user
         */
        $route = new Zend_Controller_Router_Route(
                        'admin/user/delete/:userID',
                        array(
                            'module' => 'admin',
                            'controller' => 'user',
                            'action' => 'delete'
                        )
        );
        $router->addRoute('admin_user_delete', $route);

        /*
         * Route for edit user role
         */
        $route = new Zend_Controller_Router_Route(
                        'admin/userrole/edit/:userRoleID',
                        array(
                            'module' => 'admin',
                            'controller' => 'userrole',
                            'action' => 'edit'
                        )
        );
        $router->addRoute('admin_userrole_edit', $route);

        /*
         * Route for delete user role
         */
        $route = new Zend_Controller_Router_Route(
                        'admin/userrole/delete/:userRoleID',
                        array(
                            'module' => 'admin',
                            'controller' => 'userrole',
                            'action' => 'delete'
                        )
        );
        $router->addRoute('admin_userrole_delete', $route);

        
    }

}

